from django.db.models import SET_DEFAULT
from utils.models import *


class Info(CommonModel, UserInfo):
    DoesNotExist = None
    _meta = None
    objects = None
    number = models.CharField(verbose_name='学号', max_length=128, null=True, blank=True, unique=True)
    dept = models.ManyToManyField('Dept', blank=True, null=True, verbose_name='所属部门')
    position = models.ManyToManyField('position', verbose_name='职位', null=True, blank=True, help_text='现在职务')
    power = models.ManyToManyField('Power', verbose_name='权限', null=True, blank=True, help_text='用户权限')
    credit = models.FloatField(verbose_name='操勤分', default=60, max_length=100)
    station = models.ManyToManyField('Station', verbose_name='岗位', null=True, blank=True, help_text='所属岗位')

    """默认头像"""

    def avatar_url(self):
        if self.avatar and hasattr(self.avatar, 'url'):
            return self.avatar.url
        else:
            return '/medias/default/student.jpg'

    def dept_list(self):
        return ' | '.join([i.name for i in self.dept.all()])

    dept_list.short_description = '所属部门和班级'

    class Meta:
        db_table = 'user_info'  # 数据库表名
        verbose_name = '用户信息'  # admin后台名
        verbose_name_plural = verbose_name  # 去s

    """后台显示名称"""

    def __str__(self):
        return self.name


class Dept(introduce, CommonModel):
    DoesNotExist = None
    objects = None
    owner = models.CharField(max_length=32, verbose_name="负责人", null=True, blank=True, help_text="部门负责人或者班主任")
    parent = models.ForeignKey(to='Dept', on_delete=SET_DEFAULT, default=None, verbose_name="上级部门",
                               db_constraint=False, null=True, blank=True, help_text="上级部门")
    status = models.ForeignKey(to='workflows.State', verbose_name="状态", null=True, blank=True, help_text="状态",
                               on_delete=models.DO_NOTHING)
    label = models.ForeignKey('Label', on_delete=models.CASCADE, null=True, blank=True, help_text='标记部门类型',
                              verbose_name='标签')

    class Meta:
        db_table = 'dept_info'
        verbose_name = '部门信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Station(introduce):
    objects = None

    class Meta:
        db_table = 'station_info'
        verbose_name = '岗位信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Power(introduce):
    objects = None

    class Meta:
        db_table = 'power_info'
        verbose_name = '权限信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Position(introduce):
    objects = None

    class Meta:
        db_table = 'position_info'
        verbose_name = '职位信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Label(introduce):
    objects = None

    class Meta:
        db_table = 'label_info'
        verbose_name = '标签信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
